﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Pvr_UnitySDKAPI;

namespace Assets.Scripts.TDu3DWork
{
    class TDuTriggerScript : UnityEngine.MonoBehaviour
    {
        public int m_iHand;
        public GameObject m_hand;
        public GameObject m_referenceObject;
        public Transform  m_originParent;
        public GameObject m_CenterObject;//手的中心
        public Vector3 m_vecOriginPos;
        public Vector3 m_vecOriginScale;
        public Quaternion m_vecOriginRot;

        public bool EnablePickObj;
        public GameObject m_pickObj;
        public GameObject m_copyPickObj;

        public bool IsTargetObj;//用来确定放下手中的物体时，目标物体是否为手中物体的触发对象
        private void Start()
        {
            Debug.Log("我的名字是    :" + gameObject.name);
            Debug.Log("我的父节点是    :" + gameObject.transform.parent.name);
            EnablePickObj = true;
            IsTargetObj = false;
        }

        private void LateUpdate()
        {
            if (m_iHand == 1&& Controller.UPvr_GetKeyUp(1, Pvr_KeyCode.Right)&& !EnablePickObj&& !IsTargetObj)//右手放下物体，在OnTriggerStay中拿取物体之后，在这里放下，因为拿去物体之后OnTriggerStay可能已经失效了,碰撞检测不是目标物体才恢复原位置，如果是目标物体就不执行这里
            {
                //检测当前手里是否有拿着物体

                #region 不复制

                //m_pickObj.transform.parent = m_originParent;
                //m_pickObj.transform.position = m_vecOriginPos;
                //m_pickObj.transform.localScale = m_vecOriginScale;
                //m_pickObj.transform.rotation = m_vecOriginRot;
                #endregion

                //销毁复制的物体
                DestroyImmediate(m_copyPickObj);
                m_pickObj = null;
                EnablePickObj = true;
                m_hand.GetComponent<MeshRenderer>().enabled = true;
            }
        }
      
        Vector3 dragStartPos;//记录坐标
        float startRotZ ;
        Vector3 offset;//差值
        public void OnTriggerEnter(Collider other)
        {
            Debug.Log("-----OnTriggerEnter:other name:" + other.name);
            if(other.transform.GetComponent<HighlightableObject>())
            {
                HighlightableObject ho = other.transform.GetComponent<HighlightableObject>();
                ho.FlashingOn(Color.yellow, Color.yellow, 1f);
            }

            if (other.transform.parent)
            {
                if (other.transform.parent.name == "zhudianceji" || other.transform.parent.name == "zuoyouzhu")
                {

                    dragStartPos = gameObject.transform.position;//开始坐标记录
                    startRotZ = GameObject.Find("zuoyouzhu").transform.eulerAngles.z;
                }
            }


            
        }

        public void OnTriggerExit(Collider other)
        {
            Debug.Log("-----OnTriggerExit:other name:" + other.name);
            if (other.transform.GetComponent<HighlightableObject>())
            {
                HighlightableObject ho = other.transform.GetComponent<HighlightableObject>();
                ho.FlashingOff();
            }
        }

     
        
        public void OnTriggerStay(Collider other)
        {

            //
            if(other.transform.parent)
            {
                //旋转工具
                //Debug.Log("当前物体有父物体:     "+ other.transform.parent.name);
                if (other.transform.parent.name == "zhudianceji" || other.transform.parent.name == "zuoyouzhu")
                {
                   
                    GameObject obj = GameObject.Find("zuoyouzhu").gameObject;
                    //判断当前
                    SetRototionDrag(this.gameObject, obj);
                }
            }

            //抓取物体,用右手抓取
            if(m_iHand==1)
            {
                if (Controller.UPvr_GetKeyDown(m_iHand, Pvr_KeyCode.Right))
                {
                    //如果是右手，并且现在可以拿取物体，同时触发检测到的这个物体是可以被拿起的物体
                    //bool IsPickObj = 检测字典中是否存在;
                    //检测目标物体是否存在字典中,在字典中才能拿起

                    if (m_hand && EnablePickObj/*&&IsPickObj*/)
                    {
                        IsTargetObj = false;


                        //抓取普通物体,物体存在因为场景的动画的关键帧里（旧版动画），暂时无法处理，，只能将原物体先复制一份出来
                        #region 不复制                        
                        //m_originParent = other.transform.parent;
                        //m_vecOriginPos = other.transform.position;
                        //m_vecOriginScale = other.transform.localScale;
                        //m_vecOriginRot = other.transform.rotation;
                        //m_pickObj = other.gameObject;
                        //other.transform.parent = m_hand.transform.parent;
                        #endregion

                        #region 复制
                        PickObj(other.gameObject, m_hand.transform.parent.parent);

                        #endregion


                        //other.transform.position = m_referenceObject.transform.position;
                        m_hand.GetComponent<MeshRenderer>().enabled = false;
                        Debug.Log("拿取物体" + other.name);
                        Debug.Log("m_copyPickObj" + m_copyPickObj.name+"Scale:  "+ m_copyPickObj.transform.localScale);
                        EnablePickObj = false;


                    }

                }

                //碰到其他物体之后，弹起手柄Right键，检测当前物体是否为拿在手上的物体的目标物体，如果是 则触发相应动作，这边不用来检测
                if (Controller.UPvr_GetKeyUp(m_iHand, Pvr_KeyCode.Right))
                {
                    //先判断是否为目标物体

                    //IsTargetObj = 字典判断键值是否对应;

                    //手里拿着的物体触碰到目标物体的动作在这里执行
                    if (IsTargetObj)
                    {
                        Debug.Log("触发了 " + m_pickObj + " 和 " + other.name + " 的动作");

                        //
                    }
                }
            }
           
        }

        public void OnCollisionEnter(Collision collision)
        {
            
        }

        public void OnCollisionExit(Collision collision)
        {
            
        }

        public void OnCollisionStay(Collision collision)
        {
            
        }


        float CheckAngle(float value)
        {
            if (value > 100)//过0变360
            {
                float angle = value - 360;// 0到90度旋转 0到45 跟360到315；
                return angle;
            }
            else
            {
                return value;
            }

        }

        public void SetRototionDrag(GameObject handModel,GameObject otherObj)
        {
            var currentPos = handModel.transform.position;
            float angle = Vector3.Angle(dragStartPos, currentPos); //求出两向量之间的夹角
            //otherObj.transform.up *= angle;
            print("angle====" + angle);
            Vector3 normal = Vector3.Cross(dragStartPos, currentPos);//叉乘求出法线向量

            angle *= Mathf.Sign(Vector3.Dot(-normal, otherObj.transform.up));//确定angle的正负
            //if (Camera.main.transform.localEulerAngles.x > 250)
            //{
            //    angle *= -1;
            //}
            //angle = ((int)angle / 15) * 15;
            print(startRotZ - angle);

            otherObj.transform.eulerAngles = new Vector3(otherObj.transform.eulerAngles.x, otherObj.transform.eulerAngles.y, startRotZ - angle);
        }
        void PickObj(GameObject originObj,Transform parent)//捡起物体并复制一份
        {
            m_copyPickObj = GameObject.Instantiate(originObj, parent);
            Vector3 objParentScale = originObj.transform.parent.localScale;
            Vector3 objScale = originObj.transform.localScale;
            m_copyPickObj.transform.localScale = new Vector3(objScale.x * objParentScale.x, objScale.y * objParentScale.y, objScale.z * objParentScale.z);

            m_pickObj = originObj.gameObject;
            //other.transform.position = m_referenceObject.transform.position;
            m_hand.GetComponent<MeshRenderer>().enabled = false;
        }
    }
}
